package br.com.tecnove.random.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import br.com.tecnove.random.android.util.AnimationUtil;
import br.com.tecnove.random.android.util.RandomGenerator;

public class RangeActivity extends Activity implements View.OnClickListener {

	private Button rangeButton;
	private EditText rangeText1;
	private EditText rangeText2;
	private TextView txtLastRange;
	private ScrollView view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rangelayout);
		rangeButton = (Button) findViewById(R.id.rangeButton);
		rangeText1 = (EditText) findViewById(R.id.editTextRange1);
		rangeText2 = (EditText) findViewById(R.id.editTextRange2);
		txtLastRange = (TextView) findViewById(R.id.txtLastRange);
		view = (ScrollView) findViewById(R.id.viewRangeScrollInside);

		rangeButton.setOnClickListener(this);
		// defaultRandomedView = (TextView) findViewById(R.id.defaultRandomed);
	}

	public void onClick(View v) {
		CharSequence preText = getString(R.string.youHaveRandomedToast);
		String randomedValue;
		if (v.getId() == R.id.rangeButton) {
			int value1;
			int value2;
			if (rangeText1.length()>9 || rangeText2.length()>9){
				Toast.makeText(this, getString(R.string.badInputRange), Toast.LENGTH_LONG).show();
				Log.e("BadInput", "Bad input at rangeActivity: value1("
						+ rangeText1.getText().toString() + ")  value2("
						+ rangeText2.getText().toString() + ")");
				Log.e("BadInputCause", "Value too big to fit in an Integer");
				return;
			} else {
				value1 = Integer.valueOf(rangeText1.getText().toString());
				value2 = Integer.valueOf(rangeText2.getText().toString());
				randomedValue = String.valueOf(RandomGenerator.generateFromRange(value1, value2));
				AnimationUtil.animateTextView(randomedValue, view, txtLastRange);
			}
		}
		Log.d("Sorted", "Range = " + preText);
//		Toast.makeText(this, preText + randomedValue, Toast.LENGTH_SHORT).show();

//		txtLastRange.setText(getString(R.string.youHaveRandomed).concat(randomedValue));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.appdefaultmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return ContextMenu.itemClicked(item, this);
	}
}
